package com.yuanqunsoft.alimigration.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import net.sf.json.JSONObject;

import com.opensymphony.xwork2.ActionSupport;
import com.yuanqunsoft.alimigration.bean.ApplicationBean;
import com.yuanqunsoft.alimigration.dao.support.Page;
import com.yuanqunsoft.alimigration.model.Application;
import com.yuanqunsoft.alimigration.service.IApplicationService;
import com.yuanqunsoft.alimigration.service.IModuleService;
import com.yuanqunsoft.alimigration.service.IReportService;
import com.yuanqunsoft.alimigration.utils.FilterList;
import com.yuanqunsoft.alimigration.utils.JsonResult;

public class ApplicationAction extends ActionSupport {
    private IApplicationService applicationService;
    private IModuleService moduleService;
    private IReportService reportService;
    private boolean success;
    private List<?> datas;
    private Integer appId;
    private JsonResult result;
    private String errorMsg;

    public JsonResult getResult() {
        return result;
    }

    public void setResult(JsonResult result) {
        this.result = result;
    }

    public String Page() {
        return "page";
    }

    public String addApplication() {
//        JSONObject json = new JSONObject(); 
        JSONObject json = JSONObject.fromObject(datas.get(0));
        try {
            Application application = (Application) JSONObject.toBean(json, Application.class);// 将json对象转换为javaBean
            this.getApplicationService().addApplication(application);
            List<Application> appList= new ArrayList<Application>();
            appList.add(application);
            this.setResult(new JsonResult(appList, 1));
            this.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            this.setSuccess(false);
        }
        return SUCCESS;
    }
    
    public String debug(){
        for(int i = 0 ;i<180; i++){
            Application application = new Application();
            application.setAppName("application_"+i);
            this.getApplicationService().addApplication(application);
        }
        return SUCCESS;
    }

    public String deleteApplication() {
        try {
            this.getModuleService().deleteByApplicatoinId(appId);
            this.getReportService().deleteByApplicationId(appId);
            this.getApplicationService().deleteApplication(appId);
            this.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            this.setErrorMsg(e.getMessage());
            this.setSuccess(false);
        }
        return SUCCESS;
    }

    public String applicationList() {
        String filter = ServletActionContext.getRequest().getParameter("filter");
        String sort = ServletActionContext.getRequest().getParameter("sort");
        String dir = ServletActionContext.getRequest().getParameter("dir");
        String orders = null;

        if (sort != null && dir != null)
            orders = sort + " " + dir;
        int startNo = Integer.parseInt(ServletActionContext.getRequest().getParameter("start"));
        int pageSize = Integer.parseInt(ServletActionContext.getRequest().getParameter("limit"));
        if (filter != null) {
            filter = FilterList.getSqlFilter(filter);
        }
        Page<?> pageList = this.getApplicationService().pageQuery(startNo, pageSize, filter, orders);
        setResult(new JsonResult(pageList.getResult(), pageList.getTotalCount()));
        this.setSuccess(true);
        return SUCCESS;
    }

    public Integer getAppId() {
        return appId;
    }

    public void setAppId(Integer appId) {
        this.appId = appId;
    }

    public IApplicationService getApplicationService() {
        return applicationService;
    }

    public void setApplicationService(IApplicationService applicationService) {
        this.applicationService = applicationService;
    }

    public boolean getSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public List<?> getDatas() {
        return datas;
    }

    public void setDatas(List<?> datas) {
        this.datas = datas;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public IModuleService getModuleService() {
        return moduleService;
    }

    public void setModuleService(IModuleService moduleService) {
        this.moduleService = moduleService;
    }

    public IReportService getReportService() {
        return reportService;
    }

    public void setReportService(IReportService reportService) {
        this.reportService = reportService;
    }
    
    
}
